import Vue from "vue"

import Vuex from "vuex"

Vue.use(Vuex)

export default new Vuex.Store({
	state:{
		isDialogShow:false,
		dialogObj:{
			itemId:null,
			name:"",
			address:"",
			pic:""
		},
		itemList:[
      {
        itemId:100,
        name:'百度',
        address:"www.baidu.com",
        pic:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566472014331&di=81db0acdd4b405658cfca4f23ea72b10&imgtype=0&src=http%3A%2F%2Fim1.tongbu.com%2Fappicon%2Fapp%2F985%2F85%2F382201985_40F614DF.jpg"
      },
      {
        itemId:101,
        name:'新浪',
        address:"www.sina.com",
        pic:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566472111627&di=cc7c7e637a93d2f83fea11630a5d783d&imgtype=0&src=http%3A%2F%2Fpic1.cxtuku.com%2F00%2F08%2F44%2Fb884eebe638f.jpg"
      },
    ]
	},
	mutations:{
		fixDialogShow(state,bl) {
			state.isDialogShow = bl;
		},
		fixItem(state,obj) {
			state.itemList.forEach((item) => {
			  if(item.itemId==obj.itemId) {
			  	item.name = obj.name;
			  	item.address = obj.address;
			  }
			})
			console.log(state.itemList)
		},
		addItem(state,obj) {
			let len = state.itemList.length-1;
			let maxId = state.itemList[len];
			maxId = maxId.itemId + 1;
			state.itemList.push({
				itemId:maxId,
				name:obj.name,
				address:obj.address,
				pic:""
			})
		},
		delItem(state,obj) {
			state.itemList.forEach((item,index,arr) => {
			  if(item.itemId==obj.itemId) {
			  	arr.splice(index,1)
			  }
			})
		},
		getDialogObj(state,payload) {
			if(payload.empty) {
				for(let key in state.dialogObj) {
					state.dialogObj[key] = ""
				}
			}else {
				Object.assign(state.dialogObj,payload.obj);
			}
			
			// state.isDialogShow = true;
		}
	}
})